package com.wanglj.service;

import com.wanglj.model.dos.TicketDO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 955
 * @title 购票业务层实现类
 * @date 2020/11/30 17:43
 */
@Service
public class TicketServiceImpl  extends Thread{
   static List<TicketDO>list=new ArrayList<>();
    static {
        TicketDO ticketDO=new TicketDO(4,23,"星球大战");
        TicketDO ticketDO1=new TicketDO(34,37,"内星球大战");
        TicketDO ticketDO2=new TicketDO(16,43,"外星球大战");
        TicketDO ticketDO3=new TicketDO(3,51,"里星球大战");
        TicketDO ticketDO4=new TicketDO(3,32,"大星球大战");
        list.add(ticketDO);
        list.add(ticketDO1);
        list.add(ticketDO2);
        list.add(ticketDO3);
        list.add(ticketDO4);
    }


    public synchronized String buy(String name, Integer quantity) {
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getName().equals(name)){

                if(list.get(i).getNum()!=0) {
                    list.get(i).setNum(list.get(i).getNum() - quantity);
                    return "编码："+list.get(i).getId()+"   "+"总价格："+quantity*list.get(i).getPrice();
                }else{
                    return "购票失败，当前票剩余0张";
                }
            }
        }

        return "没有找到该电影";
    }

    public static void main(String[] args) {
        TicketServiceImpl service=new TicketServiceImpl();
        System.out.println( service.buy("星球大战",2));
        System.out.println( service.buy("星球大战",2));
        System.out.println( service.buy("星球大战",2));
    }
}
